<template>
  <div class="apply-dialog">
    <div v-if="thisDiv === 1 || thisDiv === 2">
      <div class="title" style="display: flex;">
        <div style="flex: 1; text-align: left;padding-left: 30px;font-size: 25px;">
          {{ models.eventName }}
        </div>
        <div class="btn-list flex flex-row align-center">
          <el-button type="warning" size="mini" :loading="saveLoading" @click="saveAction">暂存</el-button>
          <el-button type="primary" size="mini" :loading="submitLoading" @click="submitAction">上报</el-button>
          <el-button type="info" size="mini" @click="handleClose">返回</el-button>
        </div>
      </div>
      <el-form
        ref="generateForm"
        label-suffix=":"
        size="small"
        :model="models"
        :rules="rules"
        label-position="right"
        label-width="130px"
      >
        <div >
          <title-info title="上报⼈信息" >
            <reporter-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="当事人信息" >
            <party-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="当事患者信息" >
            <patient-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="事件信息" >
            <event-info slot="left" :form-data="models" :this-div="thisDiv">
              <div slot="right" style="display: flex; width: 100%; flex-wrap: wrap; ">
                <el-form-item  label="器械操作者" prop="instrumentOperatorArr">
                  <div style="display: flex;">
                    <el-checkbox-group v-model="models.instrumentOperatorArr">
                      <el-checkbox v-for="(item, index) in instrumentOperatorList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                      <el-input
                        v-if="models.instrumentOperatorArr && models.instrumentOperatorArr.indexOf('13')  > -1"
                        :style="{ width: '200px', 'margin-left': '5px' }"
                        placeholder="请输入"
                        v-model="models.baqsjRecordMedicalinstrument.instrumentOperatorOther"
                      />
                    </el-checkbox-group>
                  </div>
                </el-form-item>
                <div style="width: 100%"/>
                <el-form-item label="器械名称" prop="baqsjRecordMedicalinstrument.instrumentName">
                  <el-input
                    :style="{ width: '200px' }"
                    placeholder="请输入"
                    v-model="models.baqsjRecordMedicalinstrument.instrumentName"
                  />
                </el-form-item>
                <el-form-item label="规格/型号" prop="baqsjRecordMedicalinstrument.instrumentCode">
                  <el-input
                    :style="{ width: '200px' }"
                    placeholder="请输入"
                    v-model="models.baqsjRecordMedicalinstrument.instrumentCode"
                  />
                </el-form-item>
                <el-form-item label="⽣产商" prop="baqsjRecordMedicalinstrument.instrumentCompany">
                  <el-input
                    :style="{ width: '200px' }"
                    placeholder="请输入"
                    v-model="models.baqsjRecordMedicalinstrument.instrumentCompany"
                  />
                </el-form-item>
                <div style="width: 100%"/>
                <el-form-item label="叙述器械情况" prop="baqsjRecordMedicalinstrument.instrumentDescription" style="width: 100%">
                  <el-input
                    :style="{ width: '50%' }"
                    :rows="3"
                    type="textarea"
                    placeholder="请输入"
                    v-model="models.baqsjRecordMedicalinstrument.instrumentDescription"
                  />
                </el-form-item>
                <div style="width: 100%"/>
                <el-form-item label="事件导致的结果（对当事⼈/当事患者）" prop="instrumentResult">
                  <el-radio-group v-model="models.baqsjRecordMedicalinstrument.instrumentResult">
                    <el-radio v-for="(item, index) in instrumentResultList" :key="index" :label="item.value">{{ item.label }}</el-radio>
                  </el-radio-group>
                  <el-input
                    v-if="models.baqsjRecordMedicalinstrument.instrumentResult && models.baqsjRecordMedicalinstrument.instrumentResult === '13'"
                    :style="{ width: '200px' }"
                    placeholder="请输入"
                    v-model="models.baqsjRecordMedicalinstrument.instrumentResultOther"
                  />
                </el-form-item>
              </div>
            </event-info>
          </title-info>
        </div>
        <div >
          <title-info title="处理措施" >
            <measure-one-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="其他信息说明" >
            <other-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
      </el-form>
    </div>
    <div v-if="thisDiv === 3">
      <div>
        <title-info title="上报⼈信息">
          <reporter-info slot="left" :form-data="models" :this-div="thisDiv" />
        </title-info>
      </div>
      <div>
        <title-info title="当事人信息">
          <party-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="当事患者信息">
          <patient-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="事件信息">
          <event-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="医疗器械情况">
          <div slot="left">
            <table>
              <tr>
                <td class="td-title">器械操作者</td>
                <td>{{ getInstrumentOperators(models.instrumentOperatorArr) || '' }}</td>
                <td class="td-title">器械名称</td>
                <td>{{ models.baqsjRecordMedicalinstrument.instrumentName || '' }}</td>
              </tr>
              <tr>
                <td class="td-title">规格/型号</td>
                <td>{{ models.baqsjRecordMedicalinstrument.instrumentCode || '' }}</td>
                <td class="td-title">⽣产商</td>
                <td>{{ models.baqsjRecordMedicalinstrument.instrumentCompany || '' }}</td>
              </tr>
              <tr>
                <td class="td-title">叙述器械情况</td>
                <td colspan="3">{{ models.baqsjRecordMedicalinstrument.instrumentDescription || '' }}</td>
              </tr>
              <tr>
                <td class="td-title">事件导致的结果（对当事⼈/当事患者）</td>
                <td colspan="3">{{ getInstrumentResult(models.baqsjRecordMedicalinstrument.instrumentResult) || '' }}</td>
              </tr>
            </table>
          </div>
        </title-info>
      </div>
      <div>
        <title-info title="处理措施">
          <measure-one-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="其他信息说明">
          <other-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
    </div>
  </div>
</template>

<script>
import { doSaveMedicalInstrument } from '@/api/report/reportForm'

import titleInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/titleInfo.vue'
import reporterInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/reporterInfo.vue'
import partyInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/partyInfo.vue'
import patientInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/patientInfo.vue'
import eventInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/eventInfo.vue'
import measureOneInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/measureOneInfo.vue'
import otherInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/otherInfo.vue'

export default {
  name: 'MedicalInstrument',
  components: { titleInfo, reporterInfo, partyInfo, patientInfo, eventInfo, measureOneInfo, otherInfo },
  data() {
    return {
      // 当前显示的div：1 dialog 2 详情 3 处置
      thisDiv: 0,
      models: {
        baqsjRecordMedicalinstrument: {
          instrumentResult: ''
        }
      },
      loading: false,
      dialogVisible: false,
      dialogDetailVisible: false,
      submitLoading: false,
      saveLoading: false,
      rules: {},
      instrumentOperatorList: [
        { value: '10', label: '专业⼈员' },
        { value: '11', label: '⾮专业⼈员' },
        { value: '12', label: '患者' },
        { value: '13', label: '其他' }
      ],
      instrumentResultList: [
        { value: '10', label: '死亡' },
        { value: '11', label: '威胁⽣命' },
        { value: '12', label: '残疾' },
        { value: '13', label: '其他' }
      ]
    }
  },
  methods: {
    handleClose(val) {
      this.dialogVisible = false
      // 传到父组件
      this.$emit('close', val)
    },
    handleDetailClose() {
      this.dialogDetailVisible = false
    },
    getDetail(info) {
      this.models = {
        ...info,
        medicalInvolvedArr: (info.baqsjRecordHandle && info.baqsjRecordHandle.medicalInvolved) ? info.baqsjRecordHandle.medicalInvolved.split(',') : [],
        otherInvolvedArr: (info.baqsjRecordHandle && info.baqsjRecordHandle.otherInvolved) ? info.baqsjRecordHandle.otherInvolved.split(',') : [],
        instrumentOperatorArr: (info.baqsjRecordMedicalinstrument && info.baqsjRecordMedicalinstrument.instrumentOperator) ? info.baqsjRecordMedicalinstrument.instrumentOperator.split(',') : []
      };
      console.log(this.models);
    },
    editInfo(ruleInfo, info) {
      this.thisDiv = 2
      this.dialogVisible = true
      this.rules = ruleInfo
      this.getDetail(info)
    },
    detail(info) {
      this.thisDiv = 3
      this.dialogDetailVisible = true
      this.getDetail(info)
    },
    open(ruleInfo, modelInfo) {
      // 弹出框
      this.thisDiv = 1
      this.rules = {
        ...ruleInfo
      }
      this.models = {
        ...modelInfo,
        eventNo: 'MedicalInstrument',
        eventName: '医疗器械相关—医疗器械事件',
        // 其他复选框
        // 处理措施=>医疗介入
        medicalInvolvedArr: [],
        //  处理措施=>其他介入
        otherInvolvedArr: [],
        // 事件差错详情 => 对象类型(不同的表单，定制不同)
        baqsjRecordMedicalinstrument: {
          instrumentResult: ''
        },
        // 事件差错详情 =>  (不同的表单，定制不同)
        instrumentOperatorArr: [],
        instrumentResultArr: []
      }
      this.dialogVisible = true
    },
    getInstrumentOperators(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.instrumentOperatorList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '13') {
            labels.push(`其他（${this.models.baqsjRecordMedicalinstrument.instrumentOperatorOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    getInstrumentResult(value) {
      if (value === undefined || value === null) return '';
      const item = this.instrumentResultList.find(item => item.value === value);
      if (item && value === '13') {
        return `其他（${this.models.baqsjRecordMedicalinstrument.instrumentResultOther || ''}）`;
      }
      return item ? item.label : '';
    },
    submitAction() {
      // 上报
      this.models.auditStatus = 2
      this.actionRequest()
    },
    saveAction(item) {
      // 草稿
      this.models.auditStatus = 1
      this.actionRequest()
    },
    actionRequest() {
      // 新增、编辑、暂存
      this.$refs['generateForm'].validate(valid => {
        if (valid) {
          this.$message({
            message: '正在操作请稍等...',
            type: 'warning'
          })
          this.saveLoading = true
          this.submitLoading = true

          // 处理复选框数据
          this.models.baqsjRecordHandle.medicalInvolved = this.models.medicalInvolvedArr.join(',');
          this.models.baqsjRecordHandle.otherInvolved = this.models.otherInvolvedArr.join(',');
          this.models.baqsjRecordMedicalinstrument.instrumentOperator = this.models.instrumentOperatorArr.join(',');
          doSaveMedicalInstrument(this.models).then(res => {
            if (res.resStatus !== 500) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.saveLoading = false
              this.submitLoading = false
              // 关闭弹出框
              this.dialogVisible = false
              this.handleClose(1)
            } else {
              this.$message({
                type: 'error',
                message: '操作失败!'
              })
              this.saveLoading = false
              this.submitLoading = false
            }
          })
        } else {
          this.$message({
            message: '请填写必填项',
            type: 'warning'
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.title {
  padding: 12px;
  border-bottom: 1px solid rgba(0, 0, 0, .1);
  position: sticky;
  top: 0;
  z-index: 2000;
  background-color: #fff;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
  table-layout: fixed;
}
td {
  border: 1px solid #ccc;
  padding: 10px;
  text-align: left;
  /* 调整行间距 */
  line-height: 1.6;
  /* 调整字体大小 */
  font-size: 18px;
  /* 调整字间距 */
  letter-spacing: 0.5px;
}

.td-title {
  width: 12%;
  background-color: #fff;
}
</style>
